#region References

using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using SpoolFileLibrary.Reader.Winspool;

#endregion

namespace SpoolFileLibrary.Reader.Hpgl {
    internal class HPGLSpoolFile : ISpoolFile {
        private readonly IList<IPage> pages;
        private int copies;

        public HPGLSpoolFile(Stream stream) {
            if(stream == null) {
                throw new ArgumentNullException("stream");
            }
            stream.Seek(0, SeekOrigin.Begin);
            this.pages = new List<IPage>();
            var streamReader = new StreamReader(stream, Encoding.UTF8, true);
            if(!this.ParseSpoolFileData(streamReader)) {
                throw new ParsingException("Failed to parse HPGL spool file.");
            }
        }

        #region ISpoolFile Members

        public bool Parse() {
            return false;
        }

        public bool Parse(bool parsePages, int scaleFactor) {
            return false;
        }

        public SpoolFileFormat SpoolFileFormat {
            get { return SpoolFileFormat.RAW; }
        }

        public PrintjobLanguage PrintjobLanguage {
            get { return PrintjobLanguage.HPGL2; }
        }

        public IList<IPage> Pages {
            get { throw new NotImplementedException(); }
        }

        public int PageCount {
            get { return 1; }
        }

        public int Copies {
            get { return this.copies; }
        }

        public bool Color {
            get { return false; }
        }

        public int PaperLength {
            get { throw new NotImplementedException(); }
        }

        public int PaperWidth {
            get { throw new NotImplementedException(); }
        }

        public bool Landscape {
            get { throw new NotImplementedException(); }
        }

        public IDeviceMode DeviceMode {
            get { throw new NotImplementedException(); }
        }

        public byte[] RawData {
            get { throw new NotImplementedException(); }
        }
        
        #endregion

        private bool ParseSpoolFileData(StreamReader streamReader) {
            IDictionary<string, string> pjlValues = SpoolFileReader.GetPJLSectionValues(streamReader);
            IDictionary<string, string> hpglCommands = this.GetHPGLCommands(streamReader);
            if(pjlValues.ContainsKey("QTY")) {
                this.copies = int.Parse(pjlValues["QTY"]);
            }

            return true;
        }

        private IDictionary<string, string> GetHPGLCommands(StreamReader streamReader) {
            var hpglCommands = new Dictionary<string, string>();
            streamReader.BaseStream.Seek(0, SeekOrigin.Begin);

            return hpglCommands;
        }
    }
}